"""
城市管理员模型
"""
from sqlalchemy import Column, String, Integer, DateTime, Text, ForeignKey, Enum as SQLEnum, DECIMAL
from sqlalchemy.sql import func
from app.core.database import Base
import enum


class CityManagerStatus(enum.Enum):
    """城市管理员状态"""
    PENDING = "pending"       # 待审核
    APPROVED = "approved"     # 已通过
    REJECTED = "rejected"     # 已拒绝
    SUSPENDED = "suspended"   # 已暂停


class CityManager(Base):
    """城市管理员表"""
    __tablename__ = "city_managers"
    
    id = Column(String(36), primary_key=True)
    user_id = Column(String(36), ForeignKey('users.id'), nullable=False, comment="用户ID")
    city_id = Column(String(100), comment="城市ID")
    city_name = Column(String(100), comment="城市名称")
    status = Column(SQLEnum(CityManagerStatus), default=CityManagerStatus.PENDING, comment="状态")
    
    # 认证信息
    id_card_front = Column(String(500), comment="身份证正面")
    id_card_back = Column(String(500), comment="身份证背面")
    background = Column(Text, comment="背景介绍")
    
    # 业绩和薪酬
    monthly_salary = Column(DECIMAL(10, 2), default=0, comment="月度补贴")
    performance_score = Column(Integer, default=0, comment="业绩评分")
    venue_count = Column(Integer, default=0, comment="管理场地数量")
    
    # 审核信息
    applied_at = Column(DateTime, default=func.now(), comment="申请时间")
    approved_at = Column(DateTime, comment="通过时间")
    approved_by = Column(String(36), comment="审核人ID")
    reject_reason = Column(Text, comment="拒绝原因")
    
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "userId": self.user_id,
            "cityId": self.city_id,
            "cityName": self.city_name,
            "status": self.status.value if isinstance(self.status, enum.Enum) else self.status,
            "idCardFront": self.id_card_front,
            "idCardBack": self.id_card_back,
            "background": self.background,
            "monthlySalary": float(self.monthly_salary) if self.monthly_salary else 0,
            "performanceScore": self.performance_score,
            "venueCount": self.venue_count,
            "appliedAt": self.applied_at.isoformat() if self.applied_at else None,
            "approvedAt": self.approved_at.isoformat() if self.approved_at else None,
            "approvedBy": self.approved_by,
            "rejectReason": self.reject_reason,
            "createdAt": self.created_at.isoformat() if self.created_at else None,
            "updatedAt": self.updated_at.isoformat() if self.updated_at else None,
        }

